#include "key.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "lcd.h"
#include "ui.h"
#include "FreeRTOS.h"
#include "task.h"
#include "ad9833.h"
#include "ec11.h"

void key_Init()
{
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_6;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOF, &GPIO_InitStruct);
}

extern char freq_change_flag;
void key_scan(void *pvParameters)
{
    while (1)
    {
        if (key1 == 0)
        {
            vTaskDelay(50);
            if (key1 == 0 && freq_change_flag == 0&&Display.out_0 == ON)
            {
                vTaskDelay(50);
                LCD_Fill(2, 2, 235, 235, LIGHTBLUE);
                UI_black();
                if (Display.wave == Sine)
                    Display.wave = Square;
                else if (Display.wave == Square)
                    Display.wave = Triangle;
                else if (Display.wave == Triangle)
                    Display.wave = Sine;
								AD9833.wave_mode = Display.wave;
                freq_change_flag = 1;
            }
        }
        else if (key2 == 0)
        {
            vTaskDelay(50);
            if (key2 == 0)
            {
                vTaskDelay(50);
                if (Display.out_1 == ON)
                {
                    Display.out_0 = OFF;
                    AD9833_Write(0x00D0);
                    Display.Amplitude_1 = Display.Amplitude;
                    freq_change_flag = 0;
                }
                else if (Display.out_1 == OFF && freq_change_flag == 0)
                {
                    Display.out_0 = ON;
                    AD9833_Write(0x0100);
                    Display.Amplitude = Display.Amplitude_1;
                    freq_change_flag = 1;
                }
            }
        }
        else if (key3 == 0)
        {
            vTaskDelay(50);
            if (key3 == 0)
            {
                vTaskDelay(50);
                if (Display.screen_light == 100)
                {
                    Display.screen_light = 60;
                }
                else if (Display.screen_light == 60)
                {
                    Display.screen_light = 30;
                }
                else if (Display.screen_light == 30)
                {
                    Display.screen_light = 0;
                }
                else if (Display.screen_light == 0)
                {
                    Display.screen_light = 100;
                }
                TIM_SetCompare2(TIM12, Display.screen_light);
            }
        }
        else if (key4 == 0)
        {
            vTaskDelay(50);
            if (key4 == 0)
            {
                vTaskDelay(50);
                if (Display.wave_speed == 1)
                {
                    Display.wave_speed = 5;
                }
                else if (Display.wave_speed == 5)
                {
                    Display.wave_speed = 10;
                }
                else if (Display.wave_speed == 10)
                {
                    Display.wave_speed = 50;
                }
                else if (Display.wave_speed == 50)
                {
                    Display.wave_speed = 1;
                }
            }
        }
        else if(K1 == 0)
        {
             vTaskDelay(50);
             if(K1==0)
             {
                 vTaskDelay(50);
                 if(AD9833.freq_d == 1)
                 {
                     AD9833.freq_d = 10;
                 }
                 else if(AD9833.freq_d == 10)
                 {
                     AD9833.freq_d = 100;
                 }
                 else if(AD9833.freq_d==100)
                 {
                    AD9833.freq_d = 1000;
                 }
                 else if(AD9833.freq_d==1000)
                 {
                    AD9833.freq_d = 1;
                 }
             }
        }
        vTaskDelay(50);
    }
}
